package com.xiaxinyu.thread.pool.service;

import com.xiaxinyu.thread.pool.commom.Task;
import com.xiaxinyu.thread.pool.config.CustomThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author XIAXINYU3
 * @date 2021/6/11
 */
@Service
@Slf4j
public class Producer {

    public void produce() {
        ThreadPoolExecutor executor = CustomThreadPool.get();
        int counter = 0;
        while (true) {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            int queueSize = executor.getQueue().size();
            if (queueSize > 2000) {
                log.info("... finish ...");
                return;
            }

            executor.execute(new Task(counter, executor));
            counter++;
        }
    }
}
